﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Drawing;
using BranchOfSansar.Sansar.Master;
using BranchOfSansar.Sansar.Enquiry;
using BranchOfSansar.Sansar.Transfer;
using System.Data;
using System.ComponentModel;
namespace BranchOfSansar
{
    internal static class Globals
    {
        public static Token Token { get; set; }
        internal static int UserId { get; set; }
        internal static string UserName { get; set; }
        public static DateTime SystemDate { get; set; }
        
        

        public static void Info(string msg)
        {
            MessageBox.Show(msg, "Мэдээлэл", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static void ShowForm(Type a)
        {
            //Type a = Type.GetType(e.Item.Tag.ToString());

            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType() == a)
                {
                    form.Activate();
                    return;
                }
            }

            Form frm;

            frm = (Form)Activator.CreateInstance(a);

            if (frm != null)
            {
                frm.MdiParent = Application.OpenForms["frmMain"];
                //frm.Text = e.Item.Caption;
                frm.Show();
            }
        }
        public static void Err(string msg)
        {
            MessageBox.Show(msg, "Алдаа", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static void Warr(string msg)
        {
            MessageBox.Show(msg, "Анхаар", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public static DialogResult Question(string msg)
        {
            return MessageBox.Show(msg, "Асуулт", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }
        public static DateTime GetDate()
        {
            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                return obj.getDate();
            }
        }
        public static void setComboSelectedItem(ComboBoxEdit comboBox, string Val)
        {
            for (int i = 0; i < comboBox.Properties.Items.Count; i++)
            {
                if (((ComboItem)comboBox.Properties.Items[i]).StrValue.Equals(Val))
                {
                    comboBox.SelectedIndex = i;
                    break;
                }
            }
        }

        public static void setComboSelectedItem(ComboBoxEdit comboBox, int value)
        {
            for (int i = 0; i < comboBox.Properties.Items.Count; i++)
            {
                if (((ComboItem)comboBox.Properties.Items[i]).IntValue == value)
                {
                    comboBox.SelectedIndex = i;
                    break;
                }
            }
        }


        public static bool DoValidation(Control page)
        {
            foreach (Control c in page.Controls)
            {
                if (c is TextEdit)
                {
                    if (c.BackColor == Color.LemonChiffon)
                    {
                        if (string.IsNullOrWhiteSpace(c.Text))
                        {
                            Warr("Өгөгдөл дутуу оруулсан байна!" + c.Name);
                            c.Select();
                            return false;
                        }
                    }
                }
                else if (c is ComboBoxEdit)
                {
                    if (c.BackColor == Color.LemonChiffon)
                    {
                        if (((ComboBoxEdit)c).SelectedItem == null)
                        {
                            Warr("Өгөгдөл дутуу оруулсан байна!" + c.Name);
                            c.Select();
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public static DataTable ToDataTable(object data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(data.GetType());
            DataTable table = new DataTable();

            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }

            object[] values = new object[props.Count];

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = props[i].GetValue(data);
            }

            table.Rows.Add(values);

            return table;
        }
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }
    }
}
